﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ASPNETX_SE;

namespace SE_Demo
{
    public partial class IndexerDForm : Form
    {
        //private IndexerD _indexer;
        private DatabaseHelper dbHelper;
        
        public IndexerDForm()
        {
            InitializeComponent();

            //_indexer = new IndexerD();
            dbHelper = new DatabaseHelper();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            string strMachine = tbMachine.Text;
            string strDatabase = tbDatabase.Text;
            string strUsername = tbUsername.Text;
            string strPassword = tbPassword.Text;

            if (!string.IsNullOrEmpty(strMachine) & !string.IsNullOrEmpty(strDatabase) & !string.IsNullOrEmpty(strUsername) & !string.IsNullOrEmpty(strPassword))
            {
                dbHelper.Machine = strMachine;
                dbHelper.Database = strDatabase;
                dbHelper.Username = strUsername;
                dbHelper.Password = strPassword;

                dbHelper.Open();
                lbTables.DataSource = dbHelper.GetAllTableNames();

                gpbLogin.Enabled = false;
                gpbTables.Enabled = true;
                HistoryMessage("数据库连接成功,选择索引表.");
            }
            else
            {
                MessageBox.Show("Not enough information!");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lbTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            //lbColumns.DataSource = dbHelper.GetAllColumnsInTable(lbTables.SelectedItem.ToString());
            List<string> lstColumns = dbHelper.GetAllColumnsInTable(lbTables.SelectedItem.ToString());
            lbColumns.Items.Clear();
            foreach (string item in lstColumns)
            {
                lbColumns.Items.Add(item);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            gpbTables.Enabled = false;
            gpbColumns.Enabled = true;

            HistoryMessage("定义索引字段.");
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            if (lbColumns.Items.Count > -1)
            {
                string column = lbColumns.SelectedItem.ToString();
                int i = lbColumns.SelectedIndex;
                lbColumns.Items.RemoveAt(i);
                lbDestine.Items.Add(column);
            }
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            if (lbDestine.Items.Count > -1)
            {
                string column = lbDestine.SelectedItem.ToString();
                int i = lbDestine.SelectedIndex;
                lbDestine.Items.RemoveAt(i);
                lbColumns.Items.Add(column);
            }
        }

        private void btnReady_Click(object sender, EventArgs e)
        {
            if (lbDestine.Items.Count > 0)
            {
                gpbColumns.Enabled = false;
                btnIndex.Enabled = true;

                HistoryMessage("单击Index开始索引.");
            }
        }

        private void HistoryMessage(string strmessage)
        {
            lbHistory.Items.Insert(0, strmessage);
        }
    }
}
